<?php
/**
 * XGPHP 轻量级PHP框架
 * @link http://xgphp.xg3.cn
 * @version 2.0.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author 讯高科技 <xungaokeji@qq.com>
*/
namespace xg;

class Exception extends \Exception {
	protected $data = [];
	protected $statusCode = 500;
	protected $response;
    public function __construct(){
		$code=0;
		$data=[];
		$message='';
		$statusCode=500;
		$previous=null;
		$args=func_get_args();
		foreach($args as $info){
			if($info instanceof \Throwable){
				$previous=$info;
			}elseif(is_int($info)){
				if(in_array($info,[404,500])){
					$statusCode=$info;
				}else{
					$code=$info;
				}
			}elseif(is_string($info)){
				$message=$info;
			}elseif(is_array($info)){
				$data=$info;
			}
		}
		$this->data = $data;
		$this->statusCode = $statusCode;
		$message=preg_replace_callback('/\{\:([a-zA-Z0-9_\-]+)\}/', function($matches) use ($data) {
			return isset($data[$matches[1]]) ? $data[$matches[1]] : $matches[0];
		}, $message);
		parent::__construct($message, $code, $previous);
    }

    public function getStatusCode() {
        return $this->statusCode;
    }

    public function getResponse() {
        return $this->response;
    }

    public function getData() {
        return $this->data;
    }
}
